package org.thanos.baseline.utils;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import java.util.function.Function;


public class ConvertDateTimeUtils {

    /**
     * Date-LocalDateTime
     */
    public static final Function<Date, LocalDateTime> DateToLocalDateTime = that
            -> LocalDateTime.ofInstant(that.toInstant(), ZoneId.systemDefault());
    /**
     * LocalDateTime-Date
     */
    public static final Function<LocalDateTime, Date> LocalDateTimeToDate = that
            -> Date.from(that.atZone(ZoneId.systemDefault()).toInstant());
    /**
     * Date-Timestamp
     */
    public static final Function<Date, Timestamp> DateToTimestamp = that -> new Timestamp(that.getTime());
    /**
     * Timestamp-Date
     */
    public static final Function<Timestamp, Date> TimestampToDate = that -> Date.from(that.toInstant());
    /**
     * LocalDateTime-Timestamp
     */
    public static final Function<LocalDateTime, Timestamp> LocalDateTimeToTimestamp = Timestamp::valueOf;
    /**
     * Timestamp-LocalDateTime
     */
    public static final Function<Timestamp, LocalDateTime> TimestampToLocalDateTime = Timestamp::toLocalDateTime;

    private ConvertDateTimeUtils() {
    }

    /**
     * 将String类型的时间，转换为LocalDateTime类型的时间
     *
     * @param dateInString String类型的时间
     * @return LocalDateTime LocalDateTime类型的时间
     */
    public static LocalDateTime stringToLocalDateTime(String dateInString) {
        return LocalDateTime.parse(dateInString, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_FMT);
    }

    public static LocalDateTime stringToLocalDateTime(String dateInString, String formatter) {
        return LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(formatter).withZone(ZoneId.systemDefault()));
    }

    public static LocalDate stringToLocalDate(String dateInString) {
        return LocalDate.parse(dateInString, DateTimeFormatterUtil.YYYY_MM_DD_FMT);
    }

    public static LocalDate stringToLocalDate(String dateInString, String formatter) {
        return LocalDate.parse(dateInString, DateTimeFormatter.ofPattern(formatter).withZone(ZoneId.systemDefault()));
    }

    /**
     * 将LocalDateTime类型的时间，转换为String类型的时间
     *
     * @param localDateTime localDateTime类型的时间
     * @return String String类型的时间
     */
    public static String localDateTimeToString(LocalDateTime localDateTime) {
        if (Objects.isNull(localDateTime)) {
            return "";
        }
        return localDateTime.format(DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_FMT);
    }

    public static String localDateToString(LocalDate localDate, String formatter) {
        if (Objects.isNull(localDate)) {
            return "";
        }
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(formatter).withZone(ZoneId.systemDefault());
        return localDate.format(timeFormatter);
    }

    // 将String类型值转换为Date类型
    public static Date stringToDate(String time) {
        LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_FMT);
        return ConvertDateTimeUtils.LocalDateTimeToDate.apply(localDateTime);
    }

    public static Date stringToDate(String time, String formatter) {
        LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(formatter).withZone(ZoneId.systemDefault()));
        return ConvertDateTimeUtils.LocalDateTimeToDate.apply(localDateTime);
    }

    public static String dateToString(Date date) {
        return localDateTimeToString(DateToLocalDateTime.apply(date));
    }

    public static String dateToString(Date date, String format) {
        if (Objects.isNull(date)) {
            return "";
        }
        return DateToLocalDateTime.apply(date).format(DateTimeFormatter.ofPattern(format));
    }
}
